﻿using BaseFramework;
using Hykon.Utility;
using LiteFramework.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Website
{
    public class HttpHelper
    {
        #region static readonly

        // Session key
        private static readonly string SK_ONLINE_UPDATE_TIME = "OnlineUpdateTime";
        private static readonly string CONST_INFO_NO_TASK = "您无权进行此操作！";

        #endregion

        #region HasTask

        /// <summary>
        /// 判断当前用户是否有权限
        /// </summary>
        /// <param name="taskCode">权限代码</param>
        /// <exception cref="AccessSecurityException">用户会话过时，将抛出登录超时的异常</exception>
        /// <returns>true有权限，false无权限</returns>
        public static bool HasTask(string taskCode)
        {
            if (ServerApplication.User == null)
            {
                throw new AccessSecurityException("登录超时，请重新登录！");
            }

            return ServerApplication.User.HasTask(taskCode != null ? taskCode.Split(';') : null);
        }

        #endregion

        #region RequestValue

        /// <summary>
        /// 判断客户端请求参数中是否存在key参数
        /// </summary>
        /// <param name="request"></param>
        /// <param name="key">参数</param>
        /// <returns>true:存在, false:不存在</returns>
        public static bool ExistRequestKey(HttpRequest request, string key)
        {
            if (string.IsNullOrEmpty(key))
                return false;

            return ExistFormKey(request, key) || ExistQueryKey(request, key);
        }

        /// <summary>
        /// 获取客户端的请求参数。数据优先级: Form / QueryString
        /// </summary>
        /// <param name="request"></param>
        /// <param name="key">参数名(忽略大小写)</param>
        /// <returns></returns>
        public static string GetRequestValue(HttpRequest request, string key)
        {
            return (string)GetRequestValue(request, key, string.Empty);
        }

        /// <summary>
        /// 获取客户端的请求参数。数据优先级: Form / QueryString
        /// </summary>
        /// <typeparam name="T">期望返回的类型</typeparam>
        /// <param name="request"></param>
        /// <param name="key">参数名(忽略大小写)</param>
        /// <returns></returns>
        public static T GetRequestValue<T>(HttpRequest request, string key)
        {
            return GetRequestValue<T>(request, key, default(T));
        }

        /// <summary>
        /// 获取客户端的请求参数。数据优先级: Form / QueryString
        /// </summary>
        /// <typeparam name="T">期望返回的类型</typeparam>
        /// <param name="request"></param>
        /// <param name="key">参数名(忽略大小写)</param>
        /// <param name="notFound">默认值（找不到时返回）</param>
        /// <returns></returns>
        public static T GetRequestValue<T>(HttpRequest request, string key, T notFound)
        {
            var value = GetFormValue(request, key);
            if (value == null || (string)value == "")
                value = GetQueryValue(request, key);

            return BaseFramework.ObjectHelper.Convert<T>(value, notFound);
        }

        #endregion

        #region Request.Form

        /// <summary>
        /// 判断客户端post表单中是否存在key属性
        /// </summary>
        /// <param name="request"></param>
        /// <param name="key">属性名(忽略大小写)</param>
        /// <returns>true:存在, false:不存在</returns>
        public static bool ExistFormKey(HttpRequest request, string key)
        {
            if (string.IsNullOrEmpty(key))
                return false;

            return request.Form.AllKeys.Count(i => string.Compare(i, key, StringComparison.OrdinalIgnoreCase) == 0) > 0;
        }

        /// <summary>
        /// 获取客户端post表单的属性key的值
        /// </summary>
        /// <param name="request"></param>
        /// <param name="key">属性名(忽略大小写)</param>
        /// <returns>返回属性值</returns>
        public static string GetFormValue(HttpRequest request, string key)
        {
            return GetFormValue(request, key, string.Empty);
        }

        /// <summary>
        /// 获取客户端post表单的属性key的值
        /// </summary>
        /// <typeparam name="T">期望返回的类型</typeparam>
        /// <param name="request"></param>
        /// <param name="key">属性名(忽略大小写)</param>
        /// <param name="defaultValue">默认值（找不到时返回）</param>
        /// <returns>返回属性值</returns>
        public static T GetFormValue<T>(HttpRequest request, string key, T defaultValue)
        {
            if (string.IsNullOrEmpty(key))
                return defaultValue;

            var value = request.Form[key];
            if (value != null)
            {
                value = HttpUtility.UrlDecode(value);
                return BaseFramework.ObjectHelper.Convert<T>(value, defaultValue);
            }

            return defaultValue;
        }

        #endregion

        #region QueryString

        /// <summary>
        /// 判断客户端get查询参数中是否存在key参数
        /// </summary>
        /// <param name="request"></param>
        /// <param name="key">参数名(忽略大小写)</param>
        /// <returns>true:存在, false:不存在</returns>
        public static bool ExistQueryKey(HttpRequest request, string key)
        {
            if (string.IsNullOrEmpty(key))
                return false;

            return request.QueryString.AllKeys.Count(i => string.Compare(i, key, StringComparison.OrdinalIgnoreCase) == 0) > 0;
        }

        /// <summary>
        /// 获取客户端get查询参数的值
        /// </summary>
        /// <param name="request"></param>
        /// <param name="key">参数名(忽略大小写)</param>
        /// <returns>返回参数值</returns>
        public static string GetQueryValue(HttpRequest request, string key)
        {
            return GetQueryValue(request, key, string.Empty);
        }

        /// <summary>
        /// 获取客户端get查询参数的值
        /// </summary>
        /// <typeparam name="T">期望返回的类型</typeparam>
        /// <param name="request"></param>
        /// <param name="key">参数名(忽略大小写)</param>
        /// <returns>返回参数值</returns>
        public static T GetQueryValue<T>(HttpRequest request, string key)
        {
            return GetQueryValue<T>(request, key, default(T));
        }

        /// <summary>
        /// 获取客户端get查询参数的值
        /// </summary>
        /// <typeparam name="T">期望返回的类型（参数数据类型）</typeparam>
        /// <param name="request"></param>
        /// <param name="key">参数名(忽略大小写)</param>
        /// <param name="defaultValue">默认值（找不到时返回）</param>
        /// <returns>返回参数值</returns>
        public static T GetQueryValue<T>(HttpRequest request, string key, T defaultValue)
        {
            if (string.IsNullOrEmpty(key))
                return defaultValue;

            var value = request.QueryString[key];
            if (value != null)
            {
                value = HttpUtility.UrlDecode(value);
                return BaseFramework.ObjectHelper.Convert<T>(value, defaultValue);
            }

            return defaultValue;
        }

        #endregion

        #region HasAction

        /// <summary>
        /// 是否存在指定的请求(action=add, 代表存在add请求)
        /// </summary>
        /// <param name="request"></param>
        /// <param name="action">请求标识</param>
        /// <returns>true:存在, false:不存在</returns>
        public static bool HasAction(HttpRequest request, ActionType action)
        {
            return HasAction(request, action.ToString());
        }

        /// <summary>
        /// 是否存在指定的请求(action=add, 代表存在add请求)
        /// </summary>
        /// <param name="request"></param>
        /// <param name="action">请求标识（不区分大小写）</param>
        /// <returns>true:存在, false:不存在</returns>
        public static bool HasAction(HttpRequest request, string action)
        {
            if (string.IsNullOrEmpty(action))
                return false;

            return (string.Compare(GetAction(request), action, true) == 0);
        }

        /// <summary>
        /// 获取请求的操作类型（?action=***，将返回***，其中***代码点位符）
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetAction(HttpRequest request)
        {
            return GetRequestValue(request, "action");
        }

        #endregion
    }
}